package config

import (
	"os"
	"sync"
)

type Config struct {
	Port   string
	DBPath string
}

func Load() *Config {
	port := os.Getenv("PORT")
	if port == "" {
		port = "8866"
	}
	
	dbPath := os.Getenv("DB_PATH")
	if dbPath == "" {
		dbPath = "data/chat.db"
	}

	return &Config{
		Port:   port,
		DBPath: dbPath,
	}
}

// In-memory API key store with env fallback
type KeyStore struct {
	mu  sync.RWMutex
	key string
}

var GlobalKeyStore KeyStore

func (ks *KeyStore) Get() string {
	ks.mu.RLock()
	defer ks.mu.RUnlock()
	if ks.key != "" {
		return ks.key
	}
	return os.Getenv("DEEPSEEK_API_KEY")
}

func (ks *KeyStore) Set(key string) {
	ks.mu.Lock()
	defer ks.mu.Unlock()
	ks.key = key
}
